package database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;



public class UserDBHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "user1.db";
    public static final String TABLE_NAME = "user_info";
    private static final int DB_VERSION = 1;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;
    private static UserDBHelper mHelper = null;

    public static UserDBHelper getInstance(Context context){
        if (mHelper == null)
            mHelper = new UserDBHelper(context);
        return mHelper;
    }
    //打开数据库的读连接
    public  SQLiteDatabase openReadLink(){
        if (mRDB == null || !mRDB.isOpen()){
            mRDB = mHelper.getReadableDatabase();
        }

        return mRDB;
    }
    //打开数据库的写连接
    public SQLiteDatabase openWriteLink(){
        if (mWDB == null ||!mWDB.isOpen()){
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }
    //关闭数据库连接
    public void closeLink(){
        if (mRDB!=null && mRDB.isOpen()){
            mRDB.close();
            mRDB = null;
        }

        if (mWDB!=null && mWDB.isOpen()){
            mWDB.close();
            mWDB = null;
        }
    }
    //构造函数
    private UserDBHelper(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
            String sql = "create table " + TABLE_NAME+"("+
                    "id integer primary key autoincrement NOT NULL," +
                    "name varchar NOT NULL," +
                    "age integer NOT NULL,"+
                    "height integer not null," +
                    "weight FLOAT NOT NULL," +
                    "married INTEGER NOT NULL);";
            db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//版本更新的时候会被调用对表结果进行修改
        String sql = "alter table " + TABLE_NAME + " add  column  phone VARCHAR;";
        db.execSQL(sql);
        sql = "alter table " + TABLE_NAME + " add column passwd varchar;";
        db.execSQL(sql);
    }


    public long deleteByname(String name){
       // mWDB.delete(TABLE_NAME,null,null); 删除所有
       return mWDB.delete(TABLE_NAME,"name=?",new String[]{name});
    }



}
